package nil.ed.livechat.timeshift.utils.m3u8;

import com.iheartradio.m3u8.*;
import com.iheartradio.m3u8.data.MediaPlaylist;
import com.iheartradio.m3u8.data.Playlist;
import org.apache.commons.io.IOUtils;

import java.io.*;

/**
 * @author delin10
 * @since 2020/6/26
 **/
public class M3u8Utils {

    public static Playlist resolve(String content) throws ParseException, PlaylistException, IOException {
        return parseInputStream(new ByteArrayInputStream(content.getBytes()));
    }

    public static Playlist parseInputStream(InputStream stream) throws ParseException, PlaylistException, IOException {
        return new PlaylistParser(
                stream,
                Format.EXT_M3U,
                Encoding.UTF_8).parse();
    }

    public static Playlist resolveFile(String path) throws ParseException, PlaylistException, IOException {
        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(path)))) {
            return parseInputStream(inputStream);
        }
    }

    public static void main(String[] args) throws ParseException, PlaylistException, IOException {
        Playlist playlist = resolveFile("D:/1.m3u8");
        MediaPlaylist mediaPlaylist = playlist.getMediaPlaylist();
        System.out.println(mediaPlaylist.getStartData().getTimeOffset());
        mediaPlaylist.getTracks().forEach(d -> {
        });
        System.out.println(playlist);
    }

}
